package com.example.testcontract.activity;

import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.testcontract.R;
import com.example.testcontract.adapter.MyPagerAdapter;
import com.example.testcontract.fragments.CollectionFragment;
import com.example.testcontract.fragments.ContactFragment;
import com.example.testcontract.fragments.DialFragment;

import java.util.ArrayList;

/**
 * Created by admin on 2014/9/18.
 */
public class MainActivity extends FragmentActivity implements View.OnClickListener {

    ViewPager viewPager;
    LinearLayout ll_dial;
    LinearLayout ll_contact;
    LinearLayout ll_collection;
    ImageView iv_cursor;
    int cursorWidth;
    int offset = 0;
    int currentIndex = 1;

    MyPagerAdapter adapter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main_layout);
        init();
        setupViewPager();
    }

    private void init() {
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        ll_dial = (LinearLayout) findViewById(R.id.bohao);
        ll_contact = (LinearLayout) findViewById(R.id.lianxiren);
        ll_collection = (LinearLayout) findViewById(R.id.shoucang);
        //iv_cursor = (ImageView) findViewById(R.id.cursor);
        initImageView();
        ll_collection.setOnClickListener(this);
        ll_contact.setOnClickListener(this);
        ll_dial.setOnClickListener(this);
    }

    private void initImageView() {
        iv_cursor = (ImageView) findViewById(R.id.cursor);
        cursorWidth = iv_cursor.getWidth();
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        int screen = displayMetrics.widthPixels;
        offset = (screen - cursorWidth * 3) / 6;//这个地方就是给每个标签左右各留出一块offset,共3个标签,6个offset,
        Matrix matrix = new Matrix();//后面我会再写一篇简要介绍Matrix的文章
        matrix.postTranslate(offset, 0);//这个就是把游标放在了第一个标签下面
        iv_cursor.setImageMatrix(matrix);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bohao:
                viewPager.setCurrentItem(0);
                break;

            case R.id.lianxiren:
                viewPager.setCurrentItem(1);
                break;

            case R.id.shoucang:
                viewPager.setCurrentItem(2);
                break;

            default:
                break;
        }
    }

    private void setupViewPager() {
        ArrayList<Fragment> fragments = new ArrayList<Fragment>();
        fragments.add(DialFragment.newInstance());
        fragments.add(ContactFragment.newInstance());
        fragments.add(CollectionFragment.newInstance());
        adapter = new MyPagerAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(1);
        viewPager.setOffscreenPageLimit(2);

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            int one = offset * 2 + cursorWidth;//卡片1 --> 卡片2 偏移量

            @Override
            public void onPageScrolled(int i, float v, int i2) {

            }

            @Override
            public void onPageSelected(int i) {
                Animation animation = new TranslateAnimation(one * (currentIndex - 1), one * (i - 1), 0, 0);//当前页面是页面1
                currentIndex = i;
                animation.setFillAfter(true);//true:图片停留在动画结束的位置
                animation.setDuration(300);
                iv_cursor.startAnimation(animation);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
